package com.techie.mdm.appbar.showcase.binding;

import org.jdesktop.beansbinding.*;
import org.springframework.binding.value.PropertyChangePublisher;
import org.springframework.binding.value.ValueModel;
import org.springframework.binding.value.support.RefreshableValueHolder;
import org.springframework.binding.value.support.ValueHolder;

import java.beans.PropertyChangeListener;

public class EmptySwitch
{

    public static ValueModel createSwitch(ValueModel primary, ValueModel secondary)
    {
        SwitchValues switchValues = new SwitchValues(primary, secondary);
        ValueHolder holder = new ValueHolder();
        Property boundProperty = ELProperty.create("${empty primary.value ? secondary.value : primary.value}");
        Property targetProperty = BeanProperty.create("value");
        Binding b = Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ,
                switchValues, boundProperty, holder, targetProperty);
        b.bind();
        return holder;
    }


    static public class SwitchValues implements PropertyChangePublisher
    {
        private Object  primary;
        private Object  secondary;

        public SwitchValues(Object primary, Object secondary)
        {
            this.primary = primary;
            this.secondary = secondary;
        }

        public Object getPrimary()
        {
            return primary;
        }

        public Object getSecondary()
        {
            return secondary;
        }

        public void addPropertyChangeListener(PropertyChangeListener listener)
        {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
        {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public void removePropertyChangeListener(PropertyChangeListener listener)
        {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
        {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    }


}
